import { setItem, getItem } from '@/utils/storage'
import { TAGS_VIEW } from '@/constant'
export default {
  namespaced: true,
  state: {
    sidebarOpend: true,
    tagsViewList: getItem(TAGS_VIEW) || []
  },
  mutations: {
    triggerSidebarOpend (state) {
      state.sidebarOpend = !state.sidebarOpend
    },
    addTagsView (state, tag) {
      // 判断新添加的tag是否已经存在tagsViewList中了
      const index = state.tagsViewList.findIndex(item => item.path === tag.path)
      // 不存在的时候才添加到tagsViewList中
      if (index === -1) {
        state.tagsViewList.push(tag)
      }
      setItem(TAGS_VIEW, state.tagsViewList)
    },
    /**
     * 移除tag标签
     * @param {*} state 状态值
     * @param { type: 'other',| 'right' | 'index', index: Number } param1 {
     */
    removeTagsView (state, { type, index }) {
      if (type === 'index') {
        state.tagsViewList.splice(index, 1)
      } else if (type === 'right') {
        state.tagsViewList.splice(index + 1)
      } else if (type === 'other') {
        state.tagsViewList.splice(index + 1)
        state.tagsViewList.splice(0, index)
      }
      setItem(TAGS_VIEW, state.tagsViewList)
    }
  }
}
